package org.eclipse.kineclipse.handlers;

import java.util.Iterator;

import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IParameter;
import org.eclipse.core.commands.IParameterValues;
import org.eclipse.core.commands.NotEnabledException;
import org.eclipse.core.commands.NotHandledException;
import org.eclipse.core.commands.ParameterValuesException;
import org.eclipse.core.commands.Parameterization;
import org.eclipse.core.commands.ParameterizedCommand;
import org.eclipse.core.commands.common.NotDefinedException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.ICommandService;
import org.eclipse.ui.handlers.IHandlerService;

public class EclipseActions {

	public static void openProblemsView(){
		IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
		ICommandService commandService = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
		Command showView = commandService
		.getCommand("org.eclipse.ui.views.showView");
		IParameter viewIdParm;
		try {
			viewIdParm = showView
			.getParameter("org.eclipse.ui.views.showView.viewId");
			IParameterValues parmValues = viewIdParm.getValues();
			String viewId = null;
			Iterator i = parmValues.getParameterValues().values().iterator();
			while (i.hasNext()) {
				String id = (String) i.next();
				if (id.indexOf("ProblemView") != -1) {
					viewId = id;
					break;
				}
			}
			Parameterization parm = new Parameterization(viewIdParm, viewId);
			ParameterizedCommand parmCommand = new ParameterizedCommand(
					showView, new Parameterization[] { parm });

			handlerService.executeCommand(parmCommand, null);
		} catch (NotDefinedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		} catch (NotEnabledException e) {
			e.printStackTrace();
		} catch (NotHandledException e) {
			e.printStackTrace();
		} catch (ParameterValuesException e) {
			e.printStackTrace();
		}
	}
	@Deprecated
	public static void newWizard(){
		IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
		try {
			handlerService.executeCommand("org.eclipse.ui.newWizard", null);
		} catch (ExecutionException e) {
			e.printStackTrace();
		} catch (NotDefinedException e) {
			e.printStackTrace();
		} catch (NotEnabledException e) {
			e.printStackTrace();
		} catch (NotHandledException e) {
			e.printStackTrace();
		}
	}
	@Deprecated
	public static void debugLast(){
		IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
		try {
			handlerService.executeCommand("org.eclipse.debug.ui.commands.DebugLast", null);
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotDefinedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotEnabledException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotHandledException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	@Deprecated	
	public static void debugStepInto(){
		IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
		try {
			handlerService.executeCommand("org.eclipse.debug.ui.commands.StepInto", null);
		} catch (ExecutionException e) {
			e.printStackTrace();
		} catch (NotDefinedException e) {
			e.printStackTrace();
		} catch (NotEnabledException e) {
			e.printStackTrace();
		} catch (NotHandledException e) {
			e.printStackTrace();
		}
	}
	@Deprecated
	public static void debugStepOver(){
		IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
		try {
			handlerService.executeCommand("org.eclipse.debug.ui.commands.StepOver", null);
		} catch (ExecutionException e) {
			e.printStackTrace();
		} catch (NotDefinedException e) {
			e.printStackTrace();
		} catch (NotEnabledException e) {
			e.printStackTrace();
		} catch (NotHandledException e) {
			e.printStackTrace();
		}
	}
	@Deprecated
	public static void debugStepReturn(){
		IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
		try {
			handlerService.executeCommand("org.eclipse.debug.ui.commands.StepReturn", null);
		} catch (ExecutionException e) {
			e.printStackTrace();
		} catch (NotDefinedException e) {
			e.printStackTrace();
		} catch (NotEnabledException e) {
			e.printStackTrace();
		} catch (NotHandledException e) {
			e.printStackTrace();
		}
	}
	@Deprecated
	public static void debugResume(){
		IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
		try {
			handlerService.executeCommand("org.eclipse.debug.ui.commands.Resume", null);
		} catch (ExecutionException e) {
			e.printStackTrace();
		} catch (NotDefinedException e) {
			e.printStackTrace();
		} catch (NotEnabledException e) {
			e.printStackTrace();
		} catch (NotHandledException e) {
			e.printStackTrace();
		}
	}
	@Deprecated
	public static void openTypeHierarchyView(){
		  IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
		  try {
		   handlerService.executeCommand("org.eclipse.jdt.ui.navigate.open.type.in.hierarchy", null);
		  } catch (ExecutionException e) {
		   e.printStackTrace();
		  } catch (NotDefinedException e) {
		   e.printStackTrace();
		  } catch (NotEnabledException e) {
		   e.printStackTrace();
		  } catch (NotHandledException e) {
		   e.printStackTrace();
		  }
	}

	public static void fakeInput() {
		// TODO Auto-generated method stub
		try {
			Thread.sleep(2000);
			System.out.println("T");
			Thread.sleep(200);
			System.out.println("e");
			Thread.sleep(500);
			System.out.println("s");
			Thread.sleep(1000);
			System.out.println("t");
		} catch (InterruptedException e) {
		
		}
	}

	public static void runCommand(String command) {
		System.out.println("Executing command " + command);
		IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
		try {
			handlerService.executeCommand(command, null);
		} catch (ExecutionException e) {
			e.printStackTrace();
		} catch (NotDefinedException e) {
			e.printStackTrace();
		} catch (NotEnabledException e) {
			e.printStackTrace();
		} catch (NotHandledException e) {
			e.printStackTrace();
		}
		
	}
	

}
